package me.lc.util.offer;

import me.lc.FunPeaceActivity;
import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.dianle.Dianle;
import com.dianle.GetTotalMoneyListener;
import com.dianle.GetUpdateMessageListener;
import com.dianle.SpendMoneyListener;

public class DianJoyOfferUtil implements IOffer, GetTotalMoneyListener,
		SpendMoneyListener, GetUpdateMessageListener {
	private boolean isInited = false;

	@Override
	public void init(Context context) {
		if (!isInited) {
			isInited = true;
			Dianle.initDianleContext((Activity) context,
					"890924340bcbf74546497bc81075da9a");
			Dianle.setCustomActivity("me.lc.util.dl.ShowImageView");
			// MyView所在的包和类名，MyView要继承DianleOfferActivity。如
			Dianle.setCustomService("me.lc.util.dl.ShowImgService");
			// MyService所在的包和类名，MyService要继承DianleOfferHelpService
			Dianle.setUpdateOnlyWifi(false);
			Dianle.getUpdateMessage(this);
		}
	}

	@Override
	public void dispose(Context context) {
	}

	@Override
	public void showOffer(Context context) {
		Dianle.showOffers();
	}

	@Override
	public void updateCoins(Context context) {
		Dianle.getTotalMoney(this);
	}

	@Override
	public void getTotalMoneyFailed(String arg0) {
	}

	@Override
	public void getTotalMoneySuccessed(String arg0, long bonus) {
		if (bonus > 0) {
			try {
				// 直接消费
				Dianle.spendMoney((int) bonus, this);
				Log.d("my dianle", "add" + bonus);
				FunPeaceActivity.Instance.coinCount += bonus;
				FunPeaceActivity.Instance.updateCoinCount();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	@Override
	public void spendMoneyFailed(String arg0) {
	}

	@Override
	public void spendMoneySuccess(long arg0) {
	}

	@Override
	public void getGetUpdateMessageFailed(String arg0) {
	}

	@Override
	public void getGetUpdateMessageSuccessed(String appUrl,
			String newVersionMessage) {
		// 开始下载，完成后提示安装.
		Dianle.autoUpdate();
	}
}
